/*
 * Created on 28.02.2006
 *
 */
package visiopuce.ideogram.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SystemUtility {
	/**
	 * Clones a serializable object with a byte stream.
	 * 
	 * This code is from the book Budio Krueger, Handbuch der
	 * Java-Programmierung, 3rd ed, 2003, Addison-Wesley, p. 949
	 * 
	 * @param obj
	 * @return the cloned object
	 */
	public static Object serialClone(Object obj) {
		Object ret = null;

		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ObjectOutputStream os;
			os = new ObjectOutputStream(out);
			os.writeObject(obj);
			os.flush();
			ByteArrayInputStream in = new ByteArrayInputStream(out
					.toByteArray());
			ObjectInputStream is = new ObjectInputStream(in);
			ret = is.readObject();
			is.close();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		return ret;
	}

}
